﻿namespace MvcXRepairAdmin.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcXRepairAdmin.Interfaces;
    using MvcXRepairAdmin.Services;
    using XRepair.Models;

    public class ModelController : BaseController
    {
        private IModelService _modelService;

        public ModelController()
        {
            _modelService = new ModelService();
        }

        public ActionResult Index()
        {
            IEnumerable<Model> modelList = _modelService.GetAllActiveModels();

            List<Model> model = modelList != null ? modelList.ToList() : new List<Model>();

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            bool succeed = _modelService.DeactivateModelByID(id);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult AddNew(Model model)
        {
            model.CreatedBy = _userSession.Username;

            bool succeed = _modelService.AddNewModel(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult Edit(Model model)
        {
            model.LastUpdatedBy = _userSession.Username;

            bool succeed = _modelService.EditModel(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }
    }
}
